<?php
/**
 * Created by PhpStorm.
 * User: 独行侠
 * Date: 2017/9/9
 * Time: 10:14
 */

namespace SDXApi\Controller;


use SDXApi\Model\OrderInvoiceModel;

class OrderInvoiceController extends AppController {

    /**
     * 补充发票
     * Created By caicm
     */
    public function supplement(){
        $order_id = I('post.order_id');
        if(empty($order_id)){
            returnApiError('参数有误');
        }

        $invoice_model = new OrderInvoiceModel();
        $invoice_model->startTrans();

        $order = $invoice_model->get_order_invoice($order_id);
        if(empty($order)){
            returnApiError('订单不存在');
        }
        if($order['invoice_id']){
            returnApiError('该订单已开过发票');
        }
        if($order['order_status'] == 2){
            returnApiError('该订单已取消，不能开发票');
        }
        if($order['pay_status'] < 2){
            returnApiError('该订单未付款，不能开发票');
        }

        $invoice_title_type = I('post.invoice_title_type', 1, 'int'); 	//抬头类型 1-个人 2-单位
        $invoice_title_content = I('post.invoice_title_content'); 	    //抬头内容（单位名称）
        $invoice_remark = I('post.invoice_remark'); 	                //用户备注

        $note = "申请开发票-补充【id {$order['invoice_id']}】".($invoice_title_type==1?'个人':'单位')."：{$invoice_title_content} 备注：{$invoice_remark} ";

        /* 记录订单操作记录 */
        $order_action = D ( 'order_action' );
        $action_data = array ();
        $action_data ['order_id'] = $order ['order_id'];
        $action_data ['action_user'] = "buyer";
        $action_data ['order_status'] = $order['order_status'];
        $action_data ['shipping_status'] = $order['shipping_status'];
        $action_data ['pay_status'] = $order['pay_status'];
        $action_data ['action_place'] = 0;
        $action_data ['action_note'] = $note;
        $action_data ['log_time'] = gmtime ();
        $action_data ['longitude'] = 0;
        $action_data ['latitude'] = 0;

        $add_action = $order_action->add ( $action_data );

        $add_inv = $invoice_model->add_invoice($order_id, 1);

        if($add_inv && $add_action){
            $invoice_model->commit();
            returnApiSuccess(null, '操作成功');

        }else{
            $invoice_model->rollback();
            returnApiError('操作失败');

        }

    }

    /**
     * 发票发送到指定邮件
     * Created By caicm
     * @param email 邮箱
     * @param invoice_id 发票id
     */
    public function sendmail(){
        $email = I('post.email');
        $invoice_id = I('post.invoice_id');
        if(empty($email) || empty($invoice_id)){
            returnApiError('参数有误');
        }
        if(!is_email($email)){
            returnApiError('邮箱格式有误');
        }

        $invoice_model = new OrderInvoiceModel();

        $invoice_info = $invoice_model->get_invoice_file($invoice_id);
        if(empty($invoice_info)){
            returnApiError('发票记录不存在');
        }
        if(empty($invoice_info['invoice_file'])){
            returnApiError('未上传发票文件');
        }

        $order_sn = substr($invoice_info['order_sn'], -8);
        $title = '食得鲜订单发票-' .$order_sn;

        $file = array(
            array(
                'url' => 'http://image.shidexian.com/'.$invoice_info['invoice_file'],
                'file_name' => $title
            )
        );

        $state = $this->send_mail($email, $title, "", $file);

        if($state){
           returnApiSuccess(null, '发送成功');
        }else{
            returnApiError('发送失败');
        }

    }

    /**
     * poptser 附带附件
     * 邮件发送
     * @param null $mail_address
     * @param null $title
     * @param null $body
     * @param null $file
     * @return bool
     */
    public function send_mail($mail_address=null, $title=null, $body=null, $file=null){
        if(empty($mail_address)||empty($title)||empty($file)){
            return false;
        }



        $mailbody = "
<table border='0' cellpadding='0' cellspacing='0' width='100%'>
<tbody><tr>
  <td bgcolor='#EFF2F7' align='center' style='padding:22px 0 36px 0;' class='padding5'>
  <table border='0' cellpadding='0' cellspacing='0' style='border-radius:4px; margin:0 auto; background-color:#ffffff' width='650' class='responsive-table'>
    <tbody>
    <tr>
      <td style='background-color:#ffffff; padding:0 50px 45px 50px;' class='padding1'>
        <table border='0' cellpadding='0' cellspacing='0' width='100%'>
           <tbody><tr>
             <td bgcolor='#ffffff' align='middle' style='background-color:#ffffff; font-size: 22px; color:#303C4B; font-weight:bold; padding:0 0 40px 0; line-height:32px;' colspan='2' class='padding4'>
               <span class='title' style='padding-bottom: 5px; border-bottom: 5px solid #ffd900;'>订单发票发送信息</span>
             </td>
          </tr>
          <tr>
             <td class='padding7' bgcolor='#ffffff' align='left' style='background-color:#ffffff; font-size: 15px; color:#4C5764; line-height:27px;' colspan='2'>
             相关内容请下载附件查看<br>
             为确保收到邮件，请将“admin@shidexian.com”加入白名单<br>
             </td>
          </tr>
        </tbody></table>
      </td>
    </tr>
        <tr>
      <td align='center' class='padding'>
        <table border='0' cellspacing='0' cellpadding='0' class='responsive-table'>
          <tbody><tr>
            <td>
              <span style='font-family:Hiragino Sans GB;font-size:16px;'>
              <a style='text-decoration:none;color:#ffffff;' href='##' target='_blank'>
               <div style='padding:10px 29px 10px 29px;border-radius:5px;text-align:center;text-decoration:none;background-color:#2bbdf8;color:#ffffff;font-size:20px;margin:0;white-space:nowrap'>食得鲜@广州优果蔬网络科技有限公司</div>
              </a>
              </span>
              <br>
            </td>
          </tr>
        </tbody></table>
      </td>
    </tr>
      </tbody></table>
    </td>
    </tr>
</tbody></table>
</td>
</tr>
</tbody></table>
                ";//邮件内容
        import('Vendor.smtp_new');
        $mail = new \MySendMail();
        $mail->setServer("smtp.ym.163.com", "admin@shidexian.com", "sdxqwe123", 465, true); //到服务器的SSL连接;
        $mail->setFrom("admin@shidexian.com");
        $send_mail_c = explode(",",$mail_address);
        foreach ($send_mail_c as $value){
            $mail->setReceiver($value);
        }
        $mail->setFileIsOss(1);
        foreach ($file as $value){
            $mail->addAttachment($value['url'], $value['file_name']);

        }
        $mail->setMail($title,$mailbody);
        return $mail->sendMail();
    }

}